//
//  TSMultiTaskDownloader.swift
//  Pods
//
//  Created by 100Years on 2025/3/17.
//

import Foundation

//public class TSMultiTaskDownloader: NSObject, URLSessionDownloadDelegate {
//    
//    public static let shared = TSMultiTaskDownloader()
//    
//    private var session: URLSession!
//    private var downloadTasks: [URLSessionDownloadTask] = []
//    private var progressHandlers: [URL: (Double) -> Void] = [:]
//    private var completionHandlers: [URL: (URL?, Error?) -> Void] = [:]
//    
//    override init() {
//        super.init()
//        let config = URLSessionConfiguration.background(withIdentifier: "com.yourapp.multitaskdownloader")
//        session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
//    }
//    
//    public func downloadFile(from url: URL, progressHandler: @escaping (Double) -> Void, completionHandler: @escaping (URL?, Error?) -> Void) {
//        let downloadTask = session.downloadTask(with: url)
//        downloadTasks.append(downloadTask)
//        progressHandlers[url] = progressHandler
//        completionHandlers[url] = completionHandler
//        downloadTask.resume()
//    }
//    
//    public func cancelDownload(for url: URL) {
//        if let task = downloadTasks.first(where: { $0.originalRequest?.url == url }) {
//            task.cancel()
//            downloadTasks.removeAll { $0.originalRequest?.url == url }
//            progressHandlers.removeValue(forKey: url)
//            completionHandlers.removeValue(forKey: url)
//        }
//    }
//    
//    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
//        guard let originalURL = downloadTask.originalRequest?.url else { return }
//        
//        let fileManager = FileManager.default
//        let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
//        let destinationURL = documentsDirectory.appendingPathComponent(originalURL.lastPathComponent)
//        
//        do {
//            try fileManager.moveItem(at: location, to: destinationURL)
//            completionHandlers[originalURL]?(destinationURL, nil)
//        } catch {
//            completionHandlers[originalURL]?(nil, error)
//        }
//        
//        downloadTasks.removeAll { $0.originalRequest?.url == originalURL }
//        progressHandlers.removeValue(forKey: originalURL)
//        completionHandlers.removeValue(forKey: originalURL)
//    }
//    
//    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
//        guard let originalURL = downloadTask.originalRequest?.url else { return }
//        
//        let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
//        progressHandlers[originalURL]?(progress)
//    }
//    
//    public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
//        guard let originalURL = task.originalRequest?.url else { return }
//        
//        if let error = error {
//            completionHandlers[originalURL]?(nil, error)
//        }
//        
//        downloadTasks.removeAll { $0.originalRequest?.url == originalURL }
//        progressHandlers.removeValue(forKey: originalURL)
//        completionHandlers.removeValue(forKey: originalURL)
//    }
//    
//    deinit {
//        session.invalidateAndCancel()
//    }
//}
